package com.google.code.irouter.portal.client.dashboard.main.center.managers.library.browser.consumer;

import com.google.code.irouter.portal.client.AppConstants;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.library.browser.consumer.editor.Form;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.library.browser.consumer.editor.form.ConsumerKey;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.library.browser.consumer.editor.form.ConsumerSecret;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.library.browser.consumer.editor.form.MaxLength;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.library.browser.consumer.editor.form.Name;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.library.browser.consumer.editor.form.Password;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.library.browser.consumer.editor.form.Quota;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.library.browser.consumer.editor.form.RecordId;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.library.browser.consumer.editor.form.Sender;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.library.browser.consumer.editor.form.Tag;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.library.browser.consumer.editor.form.User;
import com.google.code.irouter.portal.client.rpc.consumer.ConsumerResult;
import com.google.code.irouter.portal.client.rpc.consumer.ConsumerService;
import com.google.code.irouter.portal.client.rpc.consumer.ConsumerServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.Ext;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.layout.FitLayout;

public final class Editor extends Window {
	
	private static final AppConstants CONSTANTS = GWT.create(AppConstants.class);
	private final ConsumerServiceAsync consumerService = GWT.create(ConsumerService.class);
	
	public enum Mode {    
        CREATE, EDIT
    };
	
	private static Editor INSTANCE;

	public Editor() {
		super();
		this.setId(Editor.class.getName());
		
		this.setTitle(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_library_browser_consumer_editor_mode_create());  
		this.setClosable(false);  
		this.setWidth(550);  
		this.setHeight(350);  
		this.setPlain(true);
		this.setModal(true);
		this.setLayout(new FitLayout());
		this.add(new Form());
	}

	public static void turnoff() {
		if(INSTANCE != null)
			INSTANCE.hide();
	}
			
	public static void turnon(Mode mode, Element animationTarget, String... parameters) {
		if(INSTANCE == null)
			INSTANCE = new Editor();
		INSTANCE.show(animationTarget);
		if(mode.equals(Mode.CREATE)) {
			INSTANCE.setTitle(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_library_browser_consumer_editor_mode_create());
			Form form = (Form) Ext.getCmp(Form.class.getName());
			form.getForm().reset();
		} else {
			INSTANCE.setTitle(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_library_browser_consumer_editor_mode_edit());
			INSTANCE.get(parameters[0]);
		}
	}
	
	public void get(String id) {
		Form form = (Form) Ext.getCmp(Form.class.getName());
		form.setDisabled(true);
		consumerService.get(
				id, 
				new AsyncCallback<ConsumerResult>() {
					public void onFailure(Throwable caught) {
						// TODO
						Form form = (Form) Ext.getCmp(Form.class.getName());
						form.setDisabled(false);
						hide();
					}
		
					@Override
					public void onSuccess(ConsumerResult result) {
						if(result != null) {
							Form form = (Form) Ext.getCmp(Form.class.getName());
							form.edit(String.valueOf(result.getId()), result.getName(), result.getSender(), result.getConsumerKey(), result.getConsumerSecret(), result.getUser(), result.getPassword(), result.getMaxLength(), result.getQuota(), result.getTags());
							form.setDisabled(false);
						} else {
							Form form = (Form) Ext.getCmp(Form.class.getName());
							form.setDisabled(false);
							hide();
						}
					}
				}
			);
	}
	
	public void save() {
		String id = ((RecordId) Ext.getCmp(RecordId.class.getName())).getValueAsString();
		String name = ((Name) Ext.getCmp(Name.class.getName())).getValueAsString();
		String sender = ((Sender) Ext.getCmp(Sender.class.getName())).getValueAsString();
		String consumerKey = ((ConsumerKey) Ext.getCmp(ConsumerKey.class.getName())).getValueAsString();
		String consumerSecret = ((ConsumerSecret) Ext.getCmp(ConsumerSecret.class.getName())).getValueAsString();
		String user = ((User) Ext.getCmp(User.class.getName())).getValueAsString();
		String password = ((Password) Ext.getCmp(Password.class.getName())).getValueAsString();
		int maxLength = Integer.valueOf(((MaxLength) Ext.getCmp(MaxLength.class.getName())).getValueAsString());
		int quota = Integer.valueOf(((Quota) Ext.getCmp(Quota.class.getName())).getValueAsString());
		String tags = ((Tag) Ext.getCmp(Tag.class.getName())).getValueAsString();
		
		consumerService.save(
				id,
				name,
				sender,
				consumerKey,
				consumerSecret,
				user,
				password,
				maxLength,
				quota,
				tags,
				new AsyncCallback<Boolean>() {
					public void onFailure(Throwable caught) {
					// TODO
					}

					@Override
					public void onSuccess(Boolean result) {
						if(result) {
							hide();
						}						
					}
				}
		);
	}
}
